<?php
namespace app\api\controller\food\user;

use app\api\controller\food\Controller;
use app\api\model\food\Order as OrderModel;
use app\api\model\food\Goods as GoodsModel;
use app\api\model\Setting as SettingModel;
use hema\wechat\Pay as WxPay;
use hema\alipay\Driver as AlipayPay;
use think\facade\Cache;

/**
 * 用户订单管理
 */
class Order extends Controller
{
    private $user;

    /**
     * 构造方法
     */
    public function initialize()
    {
        parent::initialize();
        $this->user = $this->getUserDetail();   // 用户信息
    }

    /**
     * 我的所有类型订单列表
     */
    public function all()
    {
        $model = new OrderModel;
        $list = [
            0 => $model->getList('all',0,$this->user_id),
            1 => $model->getList('payment',0,$this->user_id),
            2 => $model->getList('comment',0,$this->user_id),
            3 => $model->getList('refund10',0,$this->user_id)
        ];
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 我的订单列表
     */
    public function lists(string $dataType)
    {
        $model = new OrderModel;
        $list = $model->getList($dataType,0,$this->user_id);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 订单详情信息
     */
    public function detail($order_id)
    {
        $order = OrderModel::getUserOrderDetail($order_id, $this->user_id);
        return $this->renderSuccess(compact('order'));
    }

    /**
     * 取消订单
     */
    public function cancel($order_id)
    {
        $model = OrderModel::getUserOrderDetail($order_id, $this->user_id);
        if ($model->cancel()) {
            return $this->renderMsg('操作成功');
        }
        $error = $model->getError() ?: '操作失败';
        return $this->renderError($error);
    }

    /**
     * 确认收货
     */
    public function receipt($order_id)
    {
        $model = OrderModel::getUserOrderDetail($order_id, $this->user_id);
        if ($model->receipt()) {
            return $this->renderMsg('操作成功');
        }
		$error = $model->getError() ?: '操作失败';
        return $this->renderError($error);
    }

    /**
     * 立即支付
	 * $pay_mode 支付模式 0微信，1余额,2餐后，3支付宝
     */
    public function pay($order_id, $pay_mode = 0)
    {
        // 订单详情
        $order = OrderModel::getUserOrderDetail($order_id, $this->user_id);
        if($data = $order->userPay($pay_mode,$this->mp)){
            return $this->renderSuccess($data);
        }
        $error = $order->getError() ?: '支付失败';
        return $this->renderError($error);
    }
    
     /**
     * 线上买单
     * $type 30=微信 40=支付宝
     */
    public function paybill($money,$type=30,$notes = '线上买单')
    {
        $order_no = order_no();
        $order = [
            'mode' => 20, //消费
            'type' => $type,//支付类型
            'order_no' => $order_no,
            'money' => $money,
            'remark' => '线上买单',
            'user_id' => $this->user_id,
            'shop_id' => $this->shop_id,
            'applet_id' => $this->applet_id
        ];
        Cache::set($order_no, $order,7200);
        //H5支付
        if($this->mp == 'h5'){
            //微信小程序支付
            if($type == 30){
                $wx = new WxPay(SettingModel::getItem('wxpay',$this->applet_id));
                if(!$result = $wx->h5($order_no,$money,'api/food.notify/paybill/appletid/'.$this->applet_id,'线上买单')){
                    return $this->renderError($wx->getError());
                }
            }
            //支付宝小程序支付
            if($type == 40){
                $alipay = new AlipayPay($this->applet_id);
                if(!$result = $alipay->tradeWapPay($order_no,$money,'api/food.notify/alipayPaybill/appletid/'.$this->applet_id,'h5/food','线上买单')){
                    $this->error = $alipay->getError();
                    return false;
                }
            }
        }else{
            //微信小程序支付
            if($type == 30){
                $wx = new WxPay(SettingModel::getItem('wxpay',$this->applet_id));
                if(!$result = $wx->jsapi($order_no,$money,$this->user['open_id'],'api/food.notify/paybill/appletid/'.$this->applet_id,'线上买单')){
                    return $this->renderError($wx->getError());
                }
            }
            //支付宝小程序支付
            if($type == 40){
                $alipay = new AlipayPay($this->applet_id);
                if(!$result = $alipay->tradeCreate($order_no,$money,$this->user['open_id'],'api/food.notify/alipayPaybill/appletid/'.$this->applet_id,'线上买单')){
                    return $this->renderError($alipay->getError());
                }
            }
        }
        return $this->renderSuccess($result);
    }
}
